﻿function no_button() {
    $("#LoginBox_two").fadeOut("fast");
    $("#LoginBox_two").css({ display: 'none' });
}
function yes_button() {
    var type = goodmanager.handletype;
    var sql;
    if (type == 1) {
        var xiugaitype;
        var dd = $.trim($("#" + goodmanager.xiugaiid).val());
        if (dd == "上线") {
            xiugaitype='0'
        }
        else if (dd == "下线") {
            xiugaitype = '2'
        }
        var id = goodmanager.xiugaiid.replace("edit", "");
        sql = "update product set Go_online='" + xiugaitype + "' where product_id='" + id + "' ";
    }
    else if (type == 0) {
        var id = goodmanager.deleteid.replace("delete", "");
        sql = "update product set deletes='0' where product_id='" + id + "' ";
    }
    else {
        return;
    }
    poststr = "{\"type\":\"handleoneexcute\",\"htmldata\":\"" + sql + "\"}";
    goodmanager.postajaxs(poststr, goodmanager.handleresult);
}


var goodmanager = {
    getdata: function (indexpage, indexwhere) {
        poststr = "{\"type\":\"goodmanager\",\"pagecurrent\":\"" + indexpage + "\",\"indexwhere\":\"" + indexwhere + "\"}";
        this.postajaxs(poststr, this.showdata);
    },
    postajaxs: function (postdata, fangfa) {
        mj_ajax("../Handler1.ashx", "json", postdata, fangfa)
    },
    xuhao: 1,
    xiugaiid: 1,
    deleteid:1,
    change_color: 1,
    //对于弹框要做的操作是下上线还是删除0是删除1是上下线
    handletype:0,
    changege_input : 1,
    showdata: function (data) {
    if (data.result == "ok")  {
        var record_count1 = data.record_count;
        record_count = record_count1 == 0 ? 1 : record_count1;
        var page_curent = data.page_curent;
        var datatable = data.table;
       
        var str = "";
        var table = document.getElementById("tbodyList");
        $("#tbodyList").empty();
        if (datatable.length != 0) {
            for (var i = 0; i < datatable.length; i++) {
                var tr = document.createElement("tr");
                if (goodmanager.change_color % 2 == 0) {
                    tr.className = "bg-info";
                    goodmanager.change_color++;
                }
                else {
                    goodmanager.change_color++;
                }
                var td2 = document.createElement("td");
                td2.innerHTML = datatable[i].product_name +"_"+ datatable[i].product_type;
                var td3 = document.createElement("td");
                td3.innerHTML = datatable[i].price;
                var td4 = document.createElement("td");
                td4.innerHTML = datatable[i].Sales_volume;
                var td5 = document.createElement("td");
                td5.innerHTML = datatable[i].Stock;
                var td6 = document.createElement("td");
                td6.innerHTML = datatable[i].productset;
                var td7 = document.createElement("td");
                td7.innerHTML = datatable[i].createtime.replace("T"," ");
                var td8 = document.createElement("td");
                td8.id="small"
                var label1 = document.createElement("input");
                label1.id = "edit" + datatable[i].product_id;
                var state = $.trim(datatable[i].Go_online);
                if (state == "2") {
                    label1.disabled = true;
                }
                label1.value = state == "0" ? "下线" : state == "1" ? "上线" : "已下线";
                label1.setAttribute("type", "button");
                label1.className = "btn btn-info input1";
                label1.onclick = function () {
                    goodmanager.xiugai(this.id);
                }
                var label2 = document.createElement("input");
                label2.id = "delete" + datatable[i].product_id;
                label2.value = "删除";
                label2.setAttribute("type", "button");
                label2.className = "btn btn-default input2";
                label2.onclick = function () {
                    goodmanager.delect(this.id);
                }
                td8.appendChild(label1);
                td8.appendChild(label2);
                tr.appendChild(td2);
                tr.appendChild(td3);
                tr.appendChild(td4);
                tr.appendChild(td5);
                tr.appendChild(td6);
                tr.appendChild(td7);
                tr.appendChild(td8);
                table.appendChild(tr);
            }
        }
        else {
            $("#tbodyList").empty();
        }
        //下面进行的是分页
        $("#record_shu").text(" 总记录数" + record_count1);
        if (record_count % 10 != 0) {
            page_shu = record_count / 10 + 1;
            page_shu = parseInt(page_shu)
        }
        else {
            page_shu = record_count / 10;
        }
        var options = {
            totalPages: page_shu,
            numberOfPages: 5,
            itemTexts: function (type, page, current) {
                switch (type) {
                    case "first":
                        return "第一页";
                    case "prev":
                        return "上一页";
                    case "next":
                        return "下一页";
                    case "last":
                        return "最后一页";
                    case "page":
                        return page;
                }
            },
            onPageClicked: function (event, originalEvent, type, page) {
                var state=$.trim($("#dateboxs").text());
                state = state == "全部" ? "" : state == "出售中" ? "0" : state == "已售完" ? "1" : state == "已下线" ? "2" : "";
                var keyword = $.trim($("#keyword").val());
                goodmanager.getdata(page,state+"|"+ keyword);
            }
        }
        $('#example').bootstrapPaginator(options);
   
    }

    else {
        layer.alert(data.msg, {
            icon: 1,
            skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
        })
    }

    },
    search: function () {
        var state = $.trim($("#dateboxs").text());
        state = state == "全部" ? "" : state == "出售中" ? "0" : state == "已售完" ? "1" : state == "已下架" ? "2" : "";
        var keyword = $.trim($("#keyword").val());
        goodmanager.getdata("1", state + "|" + keyword);
    },
    xiugai: function (id) {
        goodmanager.xiugaiid = id;
        goodmanager.handletype = 1;
        var dd =$.trim($("#" + id).val());
        if (dd == "上线") {
            dd = "确定要上线这个商品？";
        }
        else if (dd == "下线") {
            dd = "确定要下线这个商品？";
        }
        else {
            return;}
        $("#content_two").text(dd);
        $("#LoginBox_two").fadeIn("fast");
        $("#LoginBox_two").css({ display: 'block' });
    },
    handleresult:function(data) {
            if (data.result == "ok") {
                try {
                    $("#LoginBox_two").fadeOut("fast");
                    $("#LoginBox_two").css({ display: 'none' });
                    layer.alert(data.msg, {
                        icon: 1,
                        skin: 'layer-ext-moon'
                    })
                }
                catch (e) { }
            }
    },
    delect: function (id) {
        goodmanager.deleteid = id;
        goodmanager.handletype = 0;
        $("#content_two").text("确定删除这个商品？");
        $("#LoginBox_two").fadeIn("fast");
        $("#LoginBox_two").css({ display: 'block' });
    },
};

